package phoenix.web.page_objects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseWebExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.core.data_models.UserProfileDataModel.AboutMe;
import phoenix.core.data_models.UserProfileDataModel.LookingFor;
import phoenix.web.locators.base.FunnelPageLocatorsBase;

/**
 * Class provides access to web elements and actions on FunnelPage
 */
public class FunnelPage extends PageBaseWebExt
{
	/**
	 * Initializes the FunnelPage
	 * @param p_manager PhoenixManager instance
	 */
	public FunnelPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	/**
	 * Gets web elements by site
	 * @return FunnelPageLocatorsBase instance
	 */
	public FunnelPageLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_CLICKANDFLIRT:
			{
				return new phoenix.web.locators.clickandflirt.FunnelPageLocators();
			}
			case ConfigExt.SITE_ID_LOCALSGOWILD:
			{
				return new phoenix.web.locators.localsgowild.FunnelPageLocators();
			}
			case ConfigExt.SITE_ID_EBONYFLIRT:
			{
				return new phoenix.web.locators.ebonyflirt.FunnelPageLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.web.locators.cheekylovers.FunnelPageLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.web.locators.wildbuddies.FunnelPageLocators();
			}
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.web.locators.flirt.FunnelPageLocators();
			}
			case ConfigExt.SITE_ID_FOREIGNGIRLFRIEND:
			{
				return new phoenix.web.locators.foreigngirlfriend.FunnelPageLocators();
			}
			case ConfigExt.SITE_ID_BENAUGHTY:
			{
				return new phoenix.web.locators.benaughty.FunnelPageLocators();
			}
			case ConfigExt.SITE_ID_NAUGHTYFEVER:
			{
				return new phoenix.web.locators.naughtyfever.FunnelPageLocators();
			}
			case ConfigExt.SITE_ID_MATUREDATING:
			{
				return new phoenix.web.locators.maturedating.FunnelPageLocators();
			}
			default:
			{
				return null;
			}
		}
	}

	public void setScreenName(String p_screenname)
	{
		log().info("Set field screenname");
		wd().waitForPresence(elements().txtScreenName, "Field 'Screenname'");

		if (manager.isNF())
		{
			wd().wait(2);
			wd().typeAndClear(elements().txtScreenName, p_screenname);
		}
		else
		{
			wd().setElementText(elements().txtScreenName, true, p_screenname);
		}
	}

	public void setDescription(LookingFor p_looking)
	{
		log().info("Set field description");

		wd().waitForPresence(elements().txtDescription, "Field 'Description'");
		wd().setElementText(elements().txtDescription, true, p_looking.description);
	}

	public void setGender()
	{
		log().info("Set select gender");

		if (manager.isWB() || manager.isFlirt() || manager.isBN())
		{
			By p_locator = (manager.testUser().isMale()) ? elements().selectGenderFemale : elements().selectGenderMale;
			wd().waitForPresence(elements().selectGender, "Select 'Gender'");
			wd().click(elements().selectGender);
			wd().waitForVisibility(p_locator, "Select " + p_locator);
			wd().click(p_locator);
		}
		else if (manager.isCNF() || manager.isLGW() || manager.isNF())
		{
			String p_value = (manager.testUser().isMale()) ? "2" : "1";
			wd().waitForPresence(elements().selectGender, "Select 'Gender'");
			wd().selectByValue(elements().selectGender, p_value);
		}
		else if (manager.isMD())
		{
			String p_value = ((manager.testUser().isMale())) ? "2" : "1";
			wd().waitForPresence(elements().selectGender, "Select 'Gender'");
			wd().wait(1);
			wd().click(elements().selectGender);
			wd().wait(1);
			wd().click(elements().selectByLocator("gender", p_value));
		}
	}

	public void setAgeFrom(LookingFor p_looking)
	{
		log().info("Set select ageFrom");

		if (manager.isWB() || manager.isFlirt() || manager.isBN())
		{
			wd().waitForPresence(elements().selectAgeFrom, "Select age 'From'");
			wd().click(elements().selectAgeFrom);
			wd().wait(2);
			wd().click(By.xpath(elements().liAgeFrom + "[@data-item='" + p_looking.ageFrom + "']"));
		}

		else if (manager.isCNF() || manager.isLGW() || manager.isNF())
		{
			wd().waitForPresence(elements().selectAgeFrom, "Select age 'From'");
			wd().wait(1);
			wd().selectByText(elements().selectAgeFrom, "18"); // Required for proper setup
			wd().wait(1);
			wd().selectByText(elements().selectAgeFrom, p_looking.ageFrom);
			wd().wait(1);
		}
		else if (manager.isMD())
		{
			wd().waitForPresence(elements().selectAgeFrom, "Select age 'To'");
			wd().click(elements().selectAgeFrom);
			wd().wait(2);
			wd().click(elements().selectByLocator("age-from", p_looking.ageFrom));
		}
	}

	public void setAgeTo(LookingFor p_looking)
	{
		log().info("Set select ageTo");

		if (manager.isWB() || manager.isFlirt() || manager.isBN())
		{
			wd().waitForPresence(elements().selectAgeTo, "Select age 'To'");
			wd().click(elements().selectAgeTo);
			wd().wait(2);
			wd().click(By.xpath(elements().liAgeTo + "[@data-item='" + p_looking.ageTo + "']"));
		}

		else if (manager.isCNF() || manager.isLGW() || manager.isNF())
		{
			wd().waitForPresence(elements().selectAgeTo, "Select age 'To'");
			wd().selectByText(elements().selectAgeTo, p_looking.ageTo);
		}
		else if (manager.isMD())
		{
			wd().waitForPresence(elements().selectAgeTo, "Select age 'To'");
			wd().click(elements().selectAgeTo);
			wd().wait(2);
			wd().click(elements().selectByLocator("age-to", p_looking.ageTo));
		}
	}

	public void setDistance(LookingFor p_looking)
	{

		log().info("Set select distance");
		wd().waitForPresence(elements().selectDistance, "Select 'Distance'");

		if (manager.isMD())
		{
			wd().click(elements().selectDistance);
			wd().wait(1);
			wd().click(elements().selectByLocator("radius", p_looking.distance));
		}
		else
		{
			wd().selectByText(elements().selectDistance, p_looking.distance);
		}
	}

	public void setLocation(LookingFor p_looking)
	{
		log().info("Set field location");
		if (!p_looking.location.isEmpty())
		{
			wd().waitForPresence(elements().txtLocation, "Field 'Location'");
			wd().setElementText(elements().txtLocation, true, p_looking.location);
		}
		else
		{
			log().info("Looking for location is null?");
		}
	}

	public void clickNextStep()
	{
		log().info("Click button Next");

		wd().waitForPresence(elements().btnNextStep, "Select age 'To'");
		wd().wait(2);
		wd().click(elements().btnNextStep);
		wd().wait(2);
	}

	public void clickSave()
	{
		log().info("Click button Save");

		wd().waitForPresence(elements().btnSave, "Button 'Save'");
		wd().wait(2);
		wd().click(elements().btnSave);
		wd().wait(2);
	}

	public void fillStepOne(String p_screenname, LookingFor p_looking)
	{
		this.setScreenName(p_screenname);
		wd().wait(2);
		this.setGender();
		wd().wait(2);
		this.setAgeFrom(p_looking);
		wd().wait(2);
		this.setAgeTo(p_looking);
		wd().wait(2);
		this.setLocation(p_looking);

		wd().wait(2);
		// Location suggest (Chrome conflict)
		if (this.isLocationSuggestPresent(p_looking.location))
		{
			wd().wait(2);
			this.clickLocationSuggest(p_looking.location);
		}

		wd().wait(2);
		this.setDescription(p_looking);
	}

	public void setHeight(AboutMe p_about)
	{
		log().info("Set select Height");

		wd().waitForPresence(elements().selectHeight, "Select 'Height'");
		wd().selectByText(elements().selectHeight, p_about.height);
	}

	public void setWeight(AboutMe p_about)
	{
		log().info("Set select Weight");

		wd().waitForPresence(elements().selectWeight, "Select 'Weight'");
		wd().selectByText(elements().selectWeight, p_about.weight);
	}

	public void setMaritalStatus(AboutMe p_about)
	{
		log().info("Set select Marital status");

		wd().waitForPresence(elements().selectMaritalStatus, "Select 'MaritalStatus'");
		wd().selectByText(elements().selectMaritalStatus, p_about.maritalStatus);
	}

	public void setNativeLanguage(AboutMe p_about)
	{
		log().info("Set select Native language");

		wd().waitForPresence(elements().selectNativeLanguage, "Select 'NativeLanguage'");
		wd().selectByText(elements().selectNativeLanguage, p_about.nativeLanguage);
	}

	public void setSecondLanguage(AboutMe p_about)
	{
		log().info("Set select Second language");

		wd().waitForPresence(elements().selectSecondLanguage, "Select 'SecondLanguage'");
		wd().selectByText(elements().selectSecondLanguage, p_about.secondLanguage);
	}

	public void setHobbiesAndInterests(AboutMe p_about)
	{
		log().info("Set field Hobbies And Interests");

		wd().waitForPresence(elements().txtHobbiesAndInterests, "Field 'HobbiesAndInterests'");
		wd().setElementText(elements().txtHobbiesAndInterests, true, p_about.hobbiesAndInterests);
	}

	public void setPersonal(AboutMe p_about)
	{
		log().info("Set field Personal");

		wd().waitForPresence(elements().txtDescription, "Field 'Personal'");
		wd().setElementText(elements().txtDescription, true, p_about.description);
	}

	public void fillStepOneFGF(String p_screenname, AboutMe p_about)
	{
		this.setScreenName(p_screenname);
		this.setHeight(p_about);
		this.setWeight(p_about);
		this.setMaritalStatus(p_about);
		this.setNativeLanguage(p_about);
		this.setSecondLanguage(p_about);
		this.setHobbiesAndInterests(p_about);
		this.setPersonal(p_about);
	}

	public void uploadPhoto()
	{
		log().info("Upload photo");

		wd().waitForPresence(elements().blockPhotoUploader, "Block 'Photo Uploader'");

		String photo = null;

		switch (manager.testUser().getGender())
		{
			case "male":
			{
				photo = manager.file().setWaterMark(ConfigExt.UPLOAD_PHOTO_MALE);
				break;
			}

			case "female":
			{
				photo = manager.file().setWaterMark(ConfigExt.UPLOAD_PHOTO_FEMALE);
				break;
			}
		}

		wd().uploadFile(elements().inputFile, photo);
	}

	public void removePhoto() throws Exception
	{
		wd().waitForPresence(elements().btnRemovePhoto, "Button 'Remove photo'");
		wd().wait(5); // JS load

		if (!manager.isFlirt() && !manager.isMD())
		{
			WebElement blockPhotoImage = wd().getWebElement(elements().blockPhotoImage);
			manager.actionBuilder().mouseOver(blockPhotoImage);
			wd().wait(5); // JS load
		}

		this.clickRemovePhoto();
		wd().wait(30); // JS load
	}

	public boolean isPhotoPresent()
	{
		log().info("Get value of photo");
		return wd().isElementPresent(elements().blockPhotoImage);
	}

	public void waitForPhoto()
	{
		wd().waitForPresence(elements().blockPhotoImage, "Block 'Photo Image'");
	}

	public void clickRemovePhoto() throws Exception
	{
		log().info("Click Remove photo");

		if (manager.isFlirt() || manager.isMD())
		{
			wd().clickByJS(elements().btnRemovePhoto);
			return;
		}

		wd().click(elements().btnRemovePhoto);
	}

	public boolean isPhotoUploaderPresent()
	{
		return wd().isElementPresent(elements().blockPhotoPopUp);
	}

	public boolean isLocationSuggestPresent(String location)
	{
		return wd().isElementPresent(elements().clickSuggection(location));
	}

	public void clickLocationSuggest(String location)
	{
		wd().click(elements().clickSuggection(location));
	}
}
